package com.easou.ps.utils;


import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;




public class URLTools {

	private static Pattern pattern = Pattern.compile("http://([^/:]+)(:(\\d+))?(/.*)?", Pattern.CASE_INSENSITIVE);
	private static Pattern toppattern = Pattern.compile("(.*).com|(.*).net|(.*).com.cn", Pattern.CASE_INSENSITIVE);

	public static String getHostFromURL(String urlStr) {
		if (null == urlStr) {
			return "";
		}
		Matcher m = pattern.matcher(urlStr);
		if (m.matches()) {
			return m.group(1);
		} else {
			return "";
		}
	}

	public static String getTopHostFromURL(String urlStr) {
		Matcher m = pattern.matcher(urlStr);
		if (m.matches()) {
			String host = m.group(1);
			String[] temp = host.split("\\.");
			if (temp.length >= 2) {
				return temp[temp.length - 2] + "." + temp[temp.length - 1];
			} else {
				return host;
			}
		} else {
			return "";
		}
	}

	public static String getPortFromURL(String urlStr) {
		Matcher m = pattern.matcher(urlStr);
		if (m.matches()) {
			String port = m.group(3);
			if (port != null) {
				return port;
			}
		}
		return "80";
	}

	public static String urlEncode(String s) {
		try {
			String s1 = URLEncoder.encode(s, "UTF-8");
			return s1;
		} catch (Exception ex) {
			return s;
		}
	}

	public static String urlDecode(String s) {
		try {
			String s1 = URLDecoder.decode(s, "UTF-8");
			return s1;
		} catch (Exception ex) {
			return s;
		}
	}

	public static String escapeURL(String s) {
		try {
			String s1 = s.replaceAll("&", "&amp;");
			return s1;
		} catch (Exception ex) {
			return s;
		}
	}

	public static String unEscapeURL(String s) {
		try {
			String s1 = s.replaceAll("&amp;", "&");
			return s1;
		} catch (Exception ex) {
			return s;
		}
	}

	private static Pattern hostPattern = Pattern.compile("(http://)?+([^/:?&]+)(:(\\d+))?([/\\?].*)?",
	        Pattern.CASE_INSENSITIVE);
	private static Pattern topHostPostfixPattern = Pattern.compile(".com$|.net$|.net.cn$|.com.cn$|.cn$|.org$|.cc$|.edu$|.edu.cn$|.gov.cn$|.gov$|.mobi$|.ru$",
			Pattern.CASE_INSENSITIVE);

	private static String ipRegex = "((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d)";
	private static final Pattern IP_PATTERN = Pattern.compile("(" + ipRegex + ")\\.(" + ipRegex + ")\\.(" + ipRegex + ")\\.(" + ipRegex + ")");

	/**
	 * 根据Url获取顶级主机名
	 * @param url
	 * @return
	 */
	public static String getTopHost(String url) {

		String host = getHostFromURL2(url);

		Matcher m = topHostPostfixPattern.matcher(host);

		String postfix = "";
		if (m.find()) {
			postfix = host.substring(m.start());
			String tmp = host.substring(0, m.start());
			String[] tmps = tmp.split("\\.");
			if (tmps.length > 1) {
				return tmps[tmps.length - 1] + postfix;
			} else {
				return tmp + postfix;
			}

		} else {
			if (ipValid(host)) {
				return host;
			}
			if (!"".equals(host)) {
				return host;
			} else {
				return url;
			}
		}
	}

	public static String getHostFromURL2(String urlStr) {
		Matcher m = hostPattern.matcher(urlStr);
		if (m.matches()) {
			return m.group(2);
		} else {
			return "";
		}
	}

	private static boolean ipValid(String s) {
		Matcher m = IP_PATTERN.matcher(s);
		return m.matches();
	}
	
	/**
	 * 3层的首页
	 * 如 http://www.easou.com/ 为true
	 * 如  276196.qzone.qq.com   为true
	 * @param url
	 * @return
	 */
	public static boolean isThreeLayerTopPage(String url) {
		if (null != url) {
			String site = getHostFromURL2(url);
			String siteAddress = url.replaceAll("^http://|/$", "");
			boolean isTopPage = site.equals(siteAddress);
			if (isTopPage) {
				String prefix = topHostPostfixPattern.matcher(site).replaceAll("");
				isTopPage = prefix.split("\\.").length <= 2;
				return isTopPage;
			}
		}
		return false;
	}
}
